

import java.awt.*;
import java.awt.event.*;
import java.util.*;

import javax.swing.*;

import java.io.*;
import java.net.*;

public class MultiDraw1 extends JApplet  {

	protected DrawingCanvas canvas;
	protected EventListener listener;
	protected boolean isApplet = false;
	protected LinkedList ShapeObjects;
	protected MDUser mdUser;
	
  /* Constructors */

  public MultiDraw1(boolean isApplet) {
	  this.isApplet = isApplet;
	    if (!isApplet) {
	      init();
	    }
  }

  public MultiDraw1() {
  /* invoked as Applet */
    this(true);
  }

  public void init()
  {
	  String userName = JOptionPane.showInputDialog("Please provide your username:");
	  System.out.println(userName);
	  JPasswordField tPasswordField = new JPasswordField(10);
	    tPasswordField.setEchoChar('*');
	    JOptionPane.showMessageDialog ( null, tPasswordField, "Enter password", JOptionPane.OK_OPTION );
	    char[] chars = tPasswordField.getPassword();
	    String userPass = new String(chars);
	  System.out.println(userPass);
	  ShapeObjects = new LinkedList<ShapeObject>();
		mdUser = new MDUser(this);
		
	    getContentPane().setLayout(new BorderLayout());
	    
	    
	    canvas = createDrawingCanvas();
	    canvas.setBackground(Color.white);
	    getContentPane().add(canvas, BorderLayout.CENTER);
	    
	    listener = createDrawingCanvasListener(canvas);
	    canvas.addDrawingCanvasListener(listener);
	    canvas.addKeyListener((KeyListener)listener);
	    ControlPanel controlPanel = createControlPanel();
	    getContentPane().add(controlPanel, BorderLayout.SOUTH);
	    EventListener controlPanelListener = createControlPanelListener(canvas);
	    controlPanel.addControlPanelListener(controlPanelListener);
	    ToolList toolList = createToolList();
	    ToolBar toolBar = createToolBar(toolList);
	    getContentPane().add(toolBar, BorderLayout.WEST);
	    MenuBar menuBar = createMenuBar(toolList);
	    getContentPane().add (menuBar, BorderLayout.NORTH);
	    //mdUser.add(controlPanel);
	    getContentPane().add(mdUser, BorderLayout.EAST);
	    
	    canvas.setCurrentTool(new SelectTool(canvas, ShapeObjects, mdUser));
  
  }
  protected void refreshShapeObjects(){
	  ShapeObjects.clear();
	  for(int i=0; i<mdUser.ShapeObjects.size(); i++)
	    {
	    	ShapeObjects.add(mdUser.ShapeObjects.get(i));
	    }
  }
  /* Factory Methods  */


	protected ControlPanel createControlPanel() {
		return new ControlPanel();
	}
	
	protected EventListener createControlPanelListener(DrawingCanvas canvas) {
		return new ControlPanelListener(canvas, ShapeObjects);
	}
	
	
	static class AppCloser extends WindowAdapter  {
		public void windowClosing(WindowEvent e) {
			System.exit(0);
		}
	}
  protected ToolBar createToolBar(ToolList toolList) {
    return new ToolBar(toolList);
  }

  protected MenuBar createMenuBar(ToolList toolList) {
    return new MenuBar(toolList);
  }

  protected DrawingCanvas createDrawingCanvas() {
    return new DrawingCanvas();
  }

  protected EventListener createDrawingCanvasListener(DrawingCanvas canvas) {
    return new DrawingCanvasListener(canvas);
  }

  /* Configure tool list used for ToolBar and MenuBar constructon */

  protected ToolList createToolList() {
    ToolList actions = new ToolList();
    actions.add(
    		new ToolListener("Select",
    		getImageIcon("select.jpg"),
    		"Select drawing tool",
    		canvas,
    		new SelectTool(canvas, ShapeObjects, mdUser)));
    actions.add(
                new ToolListener("Freehand",
	        getImageIcon("freehand.jpg"),
	        "freehand drawing tool",
	        canvas,
		new FreehandTool(canvas, ShapeObjects, mdUser)));
    actions.add(
		new ToolListener("Line",
		getImageIcon("line.jpg"),
		"Line drawing tool",
		canvas,
		new LineTool(canvas, ShapeObjects, mdUser)));
    actions.add(
		new ToolListener("Rectangle",
		getImageIcon("rectangle.jpg"),
		"Rectangle drawing tool",
		canvas,
		new RectangleTool(canvas, ShapeObjects, mdUser)));
    actions.add(
	        new ToolListener("Oval",
	        getImageIcon("oval.jpg"),
		"Oval drawing tool",
		canvas,
		new OvalTool(canvas, ShapeObjects, mdUser)));
    actions.add(
      		new ToolListener("Text",
      		getImageIcon("text.jpg"),
      		"text drawing tool",
              	canvas,
      		new TextTool(canvas, ShapeObjects, mdUser)));	
    return actions;
  }



  protected ImageIcon getImageIcon(String fileName) {
	   if(isApplet) {
	     try {
	       URL url = new URL(getCodeBase(), fileName);
	       return new ImageIcon(url);
	     }    catch(MalformedURLException e) {
	          return null;
	     }
	   }
	   else {
	      URL url = getClass().getResource(fileName);
	      return new ImageIcon(url);
	   }
	 }
  /* Main method (for invocation as Application only)  */

  public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setTitle("MultiDraw First Iteration");
    frame.getContentPane().setLayout(new BorderLayout());
    frame.getContentPane().add(new MultiDraw1(false),
			      BorderLayout.CENTER);
    frame.addWindowListener(new AppCloser());
    frame.pack();
    frame.setSize(600, 400);
    frame.setVisible(true);
  }

}
